package com.sp.system.user.mq.controller;

import com.sp.system.user.core.producer.QueueProducer;
import com.sp.system.user.core.producer.TopicProducer;
import com.sp.system.user.core.utils.ContentResult;
import com.sp.system.user.core.vo.SendMessage;
import com.sp.system.user.exception.SpErrorController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@ResponseBody
@RestController
@RequestMapping("/activemq/")
public class SendMqRestController extends SpErrorController {

    @Autowired
    private QueueProducer queueProducer;
    @Autowired
    private TopicProducer topicProducer;

    /**
     * 发送mq topic消息接口 (暂无权限设置)
     *
     * @param sendMessage
     */
    @PostMapping("/topic/send")
    public Object sendByTopic(@RequestBody SendMessage sendMessage) {
        if (sendMessage != null) {
            if (StringUtils.isBlank(sendMessage.getQueueName()) || StringUtils.isBlank(sendMessage.getMessage())) {
                return ContentResult.errorResult("", "发送消息topic失败");
            } else {
                topicProducer.sendStringTopic(sendMessage.getQueueName(), sendMessage.getMessage());
                return ContentResult.successResult("", "发送消息topic成功");
            }
        }
        return ContentResult.errorResult("", "发送消息topic失败");
    }

    /**
     * 发送mq queue消息接口
     *
     * @param sendMessage
     */
    @PostMapping("/queue/send")
    public Object sendByQueue(@RequestBody SendMessage sendMessage) {
        if (sendMessage != null) {
            if (StringUtils.isBlank(sendMessage.getQueueName()) || StringUtils.isBlank(sendMessage.getMessage())) {
                return ContentResult.errorResult("", "发送消息queue失败");
            } else {
                queueProducer.sendStringQueue(sendMessage.getQueueName(), sendMessage.getMessage());
                return ContentResult.errorResult("", "发送消息queue成功");
            }
        }
        return ContentResult.errorResult("", "发送消息queue失败");
    }
}
